package pc

import (
	. "chess_platform/internal/common"
	"encoding/json"
	"fmt"
	"github.com/astaxie/beego/orm"
)

//权限配置

//列出所有的menu
func listAllMenu(multiOrm ...orm.Ormer) (menu []*Menu, err error) {
	o := NewOrm(multiOrm, DBChessCenter)
	sql := fmt.Sprintf(`
		SELECT
			*
		FROM
			%v
		`, TablePcMenu)
	_, err = o.Raw(sql).QueryRows(&menu)
	return
}

//找出此角色对应有权限的菜单
func roleMenu(roleId int64, multiOrm ...orm.Ormer) (menuId []int64, err error) {
	role, err := RoleInfoById(roleId, multiOrm...)
	if err != nil {
		return nil, err
	}
	if role == nil {
		return nil, nil
	}

	err = json.Unmarshal([]byte(role.Menu), &menuId)
	if err != nil {
		err = fmt.Errorf(fmt.Sprintf("角色(ID:%v)的权限错误:%v", role.Id, err))
		LogPc.Error(err.Error())
		return
	}
	return
}

//menu列出所有的父节点
func listParentMenu(multiOrm ...orm.Ormer) (parent []*Menu, err error) {
	o := NewOrm(multiOrm, DBChessCenter)
	sql := fmt.Sprintf(`
		SELECT
			*
		FROM
			%v
		WHERE
			parent_id = 0
		`, TablePcMenu)
	_, err = o.Raw(sql).QueryRows(&parent)
	return
}

//menu列出所有的子节点
func listChildMenu(multiOrm ...orm.Ormer) (child []*Menu, err error) {
	o := NewOrm(multiOrm, DBChessCenter)
	sql := fmt.Sprintf(`
		SELECT
			*
		FROM
			%v
		WHERE
			parent_id != 0
		`, TablePcMenu)
	_, err = o.Raw(sql).QueryRows(&child)
	return
}

//列出所有的menu节点，父节点，子节点
func ListMenu(multiOrm ...orm.Ormer) (parent []*Menu, child []*Menu, err error) {
	parent, err = listParentMenu(multiOrm...)
	if err != nil {
		return
	}
	child, err = listChildMenu(multiOrm...)
	return
}

//合并父子节点
func MergeMenu(parent, child []*Menu) []*Menu {
	//找到属于父亲节点的子节点
	for _, v := range parent {
		var tmpChild []*Menu
		for _, vi := range child {
			if vi.ParentId == v.Id {
				tmpChild = append(tmpChild, vi)
			}
		}
		v.Child = tmpChild
	}
	return parent
}

func IsMenuExistByName(name string, multiOrm ...orm.Ormer) (int64, bool) {
	sql := fmt.Sprintf(`
		SELECT
			id
		FROM
			%v
		WHERE
			name = ?
		LIMIT 1
		`, TablePcMenu)
	var id int64
	o := NewOrm(multiOrm, DBChessCenter)
	err := o.Raw(sql, name).QueryRow(&id)
	if CheckNoExist(err) {
		return id, false
	}
	return id, true
}

func NewMenu(data map[string]interface{}, multiOrm ...orm.Ormer) (int64, error) {
	o := NewOrm(multiOrm, DBChessCenter)
	values, sql := InsertSql(TablePcMenu, data)
	result, err := o.Raw(sql, values).Exec()
	if err != nil {
		return 0, err
	}
	return result.LastInsertId()
}

func UpdateMenu(data map[string]interface{}, id int64, multiOrm ...orm.Ormer) (int64, error) {
	o := NewOrm(multiOrm, DBChessCenter)
	condition := fmt.Sprintf(`id = %v`, id)
	values, sql := UpdateSql(TablePcMenu, data, condition)
	result, err := o.Raw(sql, values).Exec()
	if err != nil {
		return 0, err
	}
	return result.RowsAffected()
}
func DeleteMenu(id int64, multiOrm ...orm.Ormer) (int64, error) {
	o := NewOrm(multiOrm, DBChessCenter)
	condition := fmt.Sprintf("id = %v", id)
	sql := DeleteSql(TablePcMenu, condition)
	result, err := o.Raw(sql).Exec()
	if err != nil {
		return 0, err
	}
	return result.RowsAffected()
}
